package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starlink.common.PageInfoTwo;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.domain.MallBooksRasDO;
import com.starlink.model.dtomapper.MallBooksSpecificationDtoMapper;
import com.starlink.model.domain.MallBooksSpecificationDO;
import com.starlink.mapper.MallBooksSpecificationMapper;
import com.starlink.model.request.save.MallBooksSpecificationSaveRequest;
import com.starlink.model.request.MallBooksSpecificationRequest;
import com.starlink.model.util.MallBooksSpecificationConvert;
import com.starlink.service.MallBooksRasService;
import com.starlink.service.MallBooksSpecificationService;
import com.starlink.model.vo.MallBooksSpecificationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import static com.starlink.common.enums.MallBooksRasEnum.NOT_SELL;
import static com.starlink.utils.IDUtils.getId;

/**
 * @author zhanghong
 */
@Service
public class MallBooksSpecificationServiceImpl extends ServiceImpl<MallBooksSpecificationMapper, MallBooksSpecificationDO> implements MallBooksSpecificationService {
    @Autowired
    private MallBooksSpecificationMapper mallBooksSpecificationMapper;
    @Autowired
    private MallBooksRasService rasService;

    @Override
    public MallBooksSpecificationVO getByKey(long id) {
        MallBooksSpecificationVO mallBooksSpecificationVO = MallBooksSpecificationDtoMapper.INSTANCE.do2VO(mallBooksSpecificationMapper.selectDOById(id));
        return mallBooksSpecificationVO;
    }

    @Override
    public PageInfoTwo<MallBooksSpecificationVO> pageQuery(MallBooksSpecificationRequest request) {
        long count = mallBooksSpecificationMapper.pageCount(request);
        List<MallBooksSpecificationDO> list = mallBooksSpecificationMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<MallBooksSpecificationVO> mallBooksSpecificationVOList = MallBooksSpecificationConvert.buildMallBooksSpecificationVoList(list);
        return new PageInfoTwo(request.getPageNum(), request.getPageSize(), mallBooksSpecificationVOList, count);
    }

    @Override
    public List<MallBooksSpecificationVO> listQuery(MallBooksSpecificationRequest request) {
        List<MallBooksSpecificationDO> list = mallBooksSpecificationMapper.selectDOList(MallBooksSpecificationDtoMapper.INSTANCE.request2DO(request));
        List<MallBooksSpecificationVO> mallBooksSpecificationVOS = MallBooksSpecificationConvert.buildMallBooksSpecificationVoList(list);
        return mallBooksSpecificationVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(MallBooksSpecificationSaveRequest request) {
        Long count = rasService.lambdaQuery()
                .eq(MallBooksRasDO::getMallBooksId, request.getMallBooksId())
                .eq(MallBooksRasDO::getIsSell, NOT_SELL.getType())
                .count();
        request.setInventory(count);
        request.setId(getId());
        saveOrUpdate(MallBooksSpecificationDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(MallBooksSpecificationSaveRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(MallBooksSpecificationDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOById(Long id) {
        mallBooksSpecificationMapper.deleteDOById(id);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateQuantity(Map<String, Long> quantityMap) {
        mallBooksSpecificationMapper.updateQuantity(quantityMap);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addQuantity(Map<String, Long> quantityMap) {
        mallBooksSpecificationMapper.addQuantity(quantityMap);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void subQuantity(Map<String, Long> map) {
        mallBooksSpecificationMapper.subQuantity(map);
    }
}